//
//  InfoView.swift
//  TLCLockDemo
//
//  Created by run on 2024/7/16.
//

import SwiftUI
import TLCLock

class InfoModel: SheetModel {
    let info: LockInfoModel
    let data: [(String, String)]

    init(info: LockInfoModel) {
        self.info = info
        data = [
            ("rom版本", info.romVersion),
            ("锁内时间", String(info.deviceDateString)),
            ("上次授时记录的服务器时间", String(info.lastServerDateString)),
            ("键盘锁定时间", String(info.lockMinute)),
            ("键盘锁定输错次数", String(info.lockNum)),
            ("电压", info.voltage),
            ("电量", "\(info.powerPercent)%"),
            ("门锁mac地址", String(info.macAddress)),
            ("密码容量", String(info.maxPwdNum)),
            ("有效密码数量", String(info.currentPwdNum)),
            ("门卡容量", String(info.maxCardNum)),
            ("有效门卡数量", String(info.currentCardNum)),
            ("重置按钮状态", info.resetBtnStatus ? "开启" : "关闭"),
            ("自动闭锁时长", String(info.autoCloseLockTime)),
            ("防撬警报", String(describing: info.preventPickingIsSwitch)),
            ("语音类型", info.voiceType.description),
            ("语音音量", String(info.voiceVolume)),
            ("ProductId", String(info.productId)),
            ("指纹容量", String(info.maxFpNum)),
            ("有效指纹数量", String(info.currentFpNum)),
            ("卡读头状态", info.cardType.description),
            ("指纹录入次数", String(info.numberOfFingerprintEntries)),
            ("即将过期提醒", String(info.expireReminder.rawValue)),
            ("support4g", String(info.isSupport4G)),
            ("添加状态", info.status.description),
        ]
    }
}

extension LockAddStatus: Swift.CustomStringConvertible {
    public var description: String {
        switch self {
        case .awaitAdd:
            return "无密码，无蓝牙管理员"
        case .noBlueAdmin:
            return "有密码，无蓝牙管理员"
        case .normal:
            return "已添加蓝牙管理员"
        }
    }
}

extension RawValueEnum where EnumType == LockAddStatus {
    public var description: String {
        switch self {
        case .type(let type):
            return type.description
        case .other(let value):
            return "未知状态[\(value)]"
        }
    }
}

struct InfoView: View {
    let model: InfoModel

    var body: some View {
        List {
            EmptyView().padding(.top, 10)
            ForEach(0 ..< model.data.count, id: \.self) { row in
                HStack(spacing: 10) {
                    Text(model.data[row].0)
                        .font(.footnote)
                        .foregroundColor(.white)
                        .frame(minWidth: 80, maxWidth: .infinity, minHeight: 20, maxHeight: 40)
                        .background(Color.accentColor)
                        .cornerRadius(10)

                    Text(model.data[row].1)
                        .font(.footnote)
                        .foregroundColor(.white)
                        .frame(minWidth: 80, maxWidth: .infinity, minHeight: 20, maxHeight: 40)
                        .background(Color.accentColor)
                        .cornerRadius(10)
                }
            }
        }.yxrNavigationTitle("Info")
    }
}
